package cn.entity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Admin {
	private String userName;
	private String userPasswd;

	private Connection con;
	private PreparedStatement pst;
	private ResultSet rs;

	private Connection getConnect() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		con = DriverManager.getConnection(
				"jdbc:mysql://localhost:3306/gouwuche?characterEncoding=UTF-8",
				"root", "");
		return con;
	}

	public boolean validate(String name, String password) throws SQLException,
			ClassNotFoundException {

		// String sql =
		// "select name,password from admin, where name = ? and password = ?";
		String sql = "select * from admin where name = ? and password = ?";
		// java.lang.NullPointerException 这是空指针异常
		// 主要就是因为con没有初始化
		con = getConnect();
		pst = con.prepareStatement(sql);
		pst.setString(1, name);
		pst.setString(2, password);
		rs = pst.executeQuery();

		if (rs.next()) {
			return true;
		}
		return false;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPasswd() {
		return userPasswd;
	}

	public void setUserPasswd(String userPasswd) {
		this.userPasswd = userPasswd;
	}

	@Override
	public String toString() {
		return "userName=" + userName + ", userPasswd=" + userPasswd;
	}

}
